<?php
class templatesController extends AppController
{
    var $uses=array();
    var $layout='admin';
    var $helpers=array('Javascript');
    var $components = array('Auth');
    function beforeFilter()
    {
         $this->tpldir='..'.DS.'views/themed/'.'default'.DS.'html';
        $this->userScope = array('admin_leve'=>1);
        $this->authorize = 'controller';
        $this->Auth->loginError = '未能登录成功,密码或帐号错误';
        $this->Auth->authError='还未登录或者你没有管理员权限';
    }
    
    function isAuthorized()
    {
           if($this->Auth->user('admin_leve')>0)
            return true;
        else
            return false;
    }
    
    
    function admin_index()
    {
        $tpllist=array();
        $dir=opendir($this->tpldir);
        $item=0;
        while($file=readdir($dir)){
            if(is_file($this->tpldir.'/'.$file)){
                $tpllist[$item]=$file;
                $item++;
             }
        }
        $this->set('tpl',$tpllist);
        $this->pageTitle='模板管理';
    }
    
    function admin_del($filename)
    {
        $delfile=$this->tpldir.'/'.$filename;
        if(unlink($delfile))
            $this->flash('删除成功','index',5);
        else
            $this->flash('未能删除成功','index',5);
    }
    
    function admin_add()
    {
         $this->pageTitle='添加模板';
        if(!empty($_POST['filename'])){
            $newtpl=$this->tpldir.'/'.$_POST['filename'].'.ctp';
            if(file_exists($newtpl)){
                $errormsg="此模板已经存在,请换个模板名";
                $this->set('errmsg',$errormsg);
            }else if(!preg_match('/^[\d|\w]+$/',$_POST['filename'])){
                $this->set('errmsg','模板名只能由英文字母和数字组成');
            }else{
                if(file_put_contents($newtpl,$_POST['content']))
                    $this->flash('添加模板成功','index',5);
            }
        }
    }
    function admin_edit($file=null)
    {
        if(!empty($file)){
            if(file_exists($this->tpldir.'/'.$file)){
                $tplcontent=file_get_contents($this->tpldir.'/'.$file);
                $this->set('fn',$file);
                $this->set('content',$tplcontent);
                $this->pageTitle='修改模板';
            }else{
                $this->flash('不存在此模板文件','index',5);
                return;
            }
        }else{
            if(empty($_POST['filename'])){
                $this->flash('请选择要修改的模板','index',5);
                return;
            }
            if(file_put_contents($this->tpldir.'/'.$_POST['filename'],$_POST['content'])){
                $this->flash('文件修改成功','index',5);
                return;
            }else{
                $this->flash('未能修改稿成功','index',5);
                return;
            }
        }
    }
    
     //方法，生成一个自定义页的静态页
    function admin_createCustomHtml($tplname)
    {
        $outHtml=$this->requestAction('html/custompage/'.$tplname);
        if(strpos($outHtml,'error:')===0){
            $errstr=str_replace('error:','',$outHtml);
            $this->flash($errstr,'-1',5);
            return;
        }
        $tplname=substr_replace($tplname,'',0,7);
        $filepath='../../html/custom/'.$tplname.'.html';
        if(file_put_contents($filepath,$outHtml)){
            $this->flash('文件成功生成','-1',5);
            return;
        }else{
            $this->flash('文件生成失败','-1',5);
            return;
        }
    }
}
?>